package logic

import (
	"context"
	"errors"
	"taobao-merchandise/models/mysql"

	"taobao-merchandise/userrpc/internal/svc"
	"taobao-merchandise/userrpc/userrpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type GoodsDelLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewGoodsDelLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GoodsDelLogic {
	return &GoodsDelLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// 商品删除
func (l *GoodsDelLogic) GoodsDel(in *userrpc.GoodsDelRequest) (*userrpc.GoodsDelResponse, error) {
	// todo: add your logic here and delete this line

	var goods mysql.Goods
	if err := goods.FindGoodsById(mysql.DB, in.UserId); err != nil {
		return nil, err
	}
	if goods.ID == 0 {
		return nil, errors.New("此用户没有发布过商品,无权限删除")
	}
	var findGoods mysql.Goods
	if err := findGoods.FindGoodsByGoodsIdAndUserId(mysql.DB, in.GoodsId, in.UserId); err != nil {
		return nil, errors.New("此用户无权限,或此用户未发布该商品")
	}
	err := findGoods.GoodsDel(mysql.DB, in.GoodsId)
	if err != nil {
		return nil, errors.New("删除失败")
	}
	return &userrpc.GoodsDelResponse{}, nil
}
